package com.xiaoshuidi.cloud.module.iot.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.iot.api.device.vo.AmmeterInfoResModel;
import com.xiaoshuidi.cloud.module.iot.common.ResultModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.ammeter.AmmeterInfoReqModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.iot.dal.repository.DeviceRepository;
import com.xiaoshuidi.cloud.module.iot.dal.repository.TbAccountRepository;
import com.xiaoshuidi.cloud.module.iot.enums.BrandEnum;
import com.xiaoshuidi.cloud.module.iot.enums.DeviceTypeEnum;
import com.xiaoshuidi.cloud.module.iot.service.AmmeterService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：baohanzhong
 * @date ：Created 2024/8/26 9:33
 * @description：
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class HangZhouXiZiHardwareTask {

    private final static Logger LOGGER = LoggerFactory.getLogger(HangZhouXiZiHardwareTask.class);

    @Autowired
    TbAccountRepository tbAccountRepository;

    @Autowired
    DeviceRepository deviceRepository;

    @Autowired
    private AmmeterService ammeterService;

    @XxlJob("iot-hangzhouxizi-task")
    @TenantIgnore
    public void hangZhouXiZiHardwareTask(){
        List<TbAccountDo> tbAccountDos=tbAccountRepository.getApiListByBrandNo(BrandEnum.EMECO.getBrand());
        if(CollectionUtil.isNotEmpty(tbAccountDos)){
            for (TbAccountDo tbAccountDo : tbAccountDos) {
                String apiNo = tbAccountDo.getApiNo();
                //查询该tb_account下挂的所有设备
                List<TbDeviceDo> deviceListByOneApiNo = deviceRepository.getDeviceListByOneApiNo(apiNo);
                //水表设备 该对接只有水表其他的默认不处理
                List<TbDeviceDo> ammeterList = deviceListByOneApiNo.stream().filter(tbDeviceDo ->
                        DeviceTypeEnum.AMMETER.getCode().equals(tbDeviceDo.getDeviceType())).collect(Collectors.toList());
                //刷新电表
                for (TbDeviceDo tbDeviceDo : ammeterList){
                    try {
                        AmmeterInfoReqModel ammeterInfoReqModel=new AmmeterInfoReqModel();
                        ammeterInfoReqModel.setDeviceNo(tbDeviceDo.getDeviceNo());
                        ResultModel<AmmeterInfoResModel> ammeterInfoResModelResultModel=ammeterService.refreshAmmeterInfo(ammeterInfoReqModel);
                        LOGGER.info("Single-定时任务杭州西子电表刷新结果:{}", JSONObject.toJSONString(ammeterInfoResModelResultModel));
                    }catch (Exception e){
                        LOGGER.error("All-定时任务杭州西子电表刷新结果,错误原因：{}", e.getMessage());
                    }
                }
            }
        }

    }


}
